import { IPagination } from '@/types/pagination'
import { Ref, ref } from 'vue'

const initialState: IPagination = {
    current: 1,
    size: 10,
    total: 1,
}

function usePagination(state: Partial<IPagination> = initialState) {
    const mergedState = Object.assign(initialState, state)

    console.log('mergedState', mergedState)

    function resetPagination(paginationRef: Ref<IPagination>) {
        paginationRef.value = mergedState
    }

    function setPagination(
        paginationRef: Ref<IPagination>,
        pagination: IPagination,
    ) {
        const { current, size, total } = pagination

        paginationRef.value = {
            current,
            size,
            total,
        }
    }

    function getNewPagination() {
        return ref<IPagination>(mergedState)
    }

    return {
        resetPagination,
        setPagination,
        getNewPagination,
    }
}

export default usePagination
